package com.xxx.video.mine.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.PlayVoiceUtils;
import com.xxx.video.commen.utils.RecordVoiceUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.mine.beans.UploadFileBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.io.File;

public class AddAudioPresenter extends BasePresenter<IAddAudioView> {

    public AddAudioPresenter(@NonNull Context context, IAddAudioView baseView) {
        super(context, baseView);
    }

    /**
     * submit
     *
     * @param time time
     */
    public void submit(int time) {
        PlayVoiceUtils.getInstance().stopVoice(true);
        RecordVoiceUtils.getInstance().stopRecord();
        String recordVoicePath = getBaseView() == null ? "" : getBaseView().getRecordVoicePath();
        String audioTitle = getBaseView() == null ? "" : getBaseView().getAudioTitle();

        if (TextUtils.isEmpty(recordVoicePath)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_record_voice_path_can_not_empty));
            return;
        }
        if (TextUtils.isEmpty(audioTitle)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_audio_title_can_not_empty));
            return;
        }
        showProgressDialog("loading");
        uploadRecordVoicePath(recordVoicePath, audioTitle, time);
    }

    /**
     * upload voice path
     *
     * @param recordVoicePath voice path
     * @param audioTitle      title
     * @param time            time
     */
    public void uploadRecordVoicePath(String recordVoicePath, String audioTitle, int time) {
        NetWorkUtils.getInstance().uploadFile(new File(recordVoicePath), new CommonResponseListener<UploadFileBean>() {
            @Override
            public void onSuccess(UploadFileBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    release(successOb.url, audioTitle, time);
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_video_path_upload_error));
            }
        });
    }

    /**
     * release
     *
     * @param recordVoicePath recordVoicePath
     * @param audioTitle      title
     * @param time            time
     */
    public void release(String recordVoicePath, String audioTitle, int time) {
        NetWorkUtils.getInstance().release(recordVoicePath, "", audioTitle, "0", String.valueOf(time / 1000), new CommonResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().releaseSuccess();
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_add_video_error));
            }
        });
    }

}
